home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / geomutil / math2oogl / OOGL.m < prev   
Text File  |  1993-10-27  |  11KB  |  297 lines

  1. (* $Header: /usrb/gcg/ngrap/src/bin/geomutil/math2oogl/RCS/OOGL.m,v 1.10 1993/10/28 03:35:01 slevy Exp $ *)
  2.  
  3. BeginPackage["OOGL`"]
  4.  
  5. (*
  6.  
  7. If you want to be able to use this package from a directory other
  8. than the geomview distribution directory, or if you want to be able to
  9. run it from a remote host, then
  10. EITHER
  11.    ensure that the programs
  12.     "geomview", "math2oogl", "togeomview", and "oogl2rib"
  13.    are in your search path (e.g. copy them to /usr/local/bin),
  14.  
  15. OR
  16.  
  17.   - Change the value of $GeomviewPath below to be the absolute pathname
  18.     of the "geomview" shell script on your system and
  19.   - Change the value of $GeomRoot below to be the absolute pathname
  20.     of the geomview distribution directory on your system.
  21.  
  22. The first alternative is preferred when possible.
  23. *)
  24.  
  25. $GeomviewPath = "geomview";
  26. $GeomRoot = ".";
  27.  
  28. (* DO NOT CHANGE ANYTHING BELOW HERE *)
  29.  
  30.  
  31. (*******************************************************************)
  32.  
  33. (* Copyright (c) 1993 The Geometry Center; University of Minnesota
  34.    1300 South Second Street Suite 500;  Minneapolis, MN  55454, USA;
  35.    
  36. This is file OOGL.m.  OOGL.m is free software.  You can redistribute
  37. it and/or modify it only under the terms given in the file COPYING,
  38. which you should have received along with this file.  this and other
  39. related software may be obtained via anonymous ftp from geom.umn.edu;
  40. email: software@geom.umn.edu. *)
  41.  
  42.  
  43. (* Authors: Nils McCarthy (nils@geom.umn.edu) 
  44.         Stuart Levy (slevy@geom.umn.edu)
  45.         Tamara Munzner (munzner@geom.umn.edu) 
  46.         Silvio Levy (levy@geom.umn.edu)
  47. *)
  48.  
  49.  
  50. (* To use this, you also need to install the math2oogl and oogl2rib C programs.
  51.    They actually do the grunt work, as Mathematica is too slow
  52.    to do it in any reasonable amount of time. *)
  53.  
  54. WriteOOGL::usage = "
  55. WriteOOGL[filename,-Graphics3D or SurfaceGraphics-] writes the given
  56. graphics object or list of graphics objects to filename.  Graphics3D
  57. objects are converted to OOGL OFF format, SurfaceGraphics objects are
  58. converted to OOGL MESH format. WriteOOGL understands only Polygon,
  59. Line, and RGBColor Graphics3D directives.  If WriteOOGL encounters any
  60. errors in the Graphics3D object, it silently tries to cope. Depends on
  61. the C program math2oogl.";
  62.  
  63. Geomview::usage= "
  64. Geomview [-Graphics3D or SurfaceGraphics-]\n 
  65. Geomview [\"name\", -Graphics3D or SurfaceGraphics-] writes the given
  66. graphics object or list of graphics objects to geomview, starting a
  67. copy of geomview if necessary.  Graphics3D objects are converted to
  68. OOGL OFF format, SurfaceGraphics objects are converted to OOGL MESH
  69. format.  Polygons, Lines, Points, and RGBColor Graphics3D directives
  70. are understood.  Geomview[\"name\", ...]  creates geomview object with
  71. that name; default is \"Mathematica\".  Depends on the C program
  72. math2oogl.";
  73.  
  74. WriteRIB::usage= "
  75. WriteRIB [filename,-Graphics3D or SurfaceGraphics-]\n 
  76. WriteRIB [filename,-Graphics3D or SurfaceGraphics-, \"options\"]]
  77. writes the given graphics object or list of graphics objects as
  78. RenderMan .rib files. It actually converts the Mathematica graphics
  79. object to the OOGL data format, and then converts the OOGL files to
  80. rib format.  Graphics3D objects become lists of Polygons, and
  81. SurfaceGraphics objects become PatchMeshes. Polygons, Lines, Points,
  82. and RGBColor Graphics3D directives are understood. Depends on the C
  83. programs math2oogl and oogl2rib. WriteRIB[ ..., \"options\"] appends
  84. the given text string to the invocation of oogl2rib.";
  85.  
  86. WriteChunk::usage= "
  87.  
  88. WriteChunk [filename,-Graphics3D or SurfaceGraphics-] is only for use
  89. on machines that cannot start up a remote Unix shell on a machine that
  90. has math2oogl, oogl2rib, and/or geomview installed (i.e. Macs, PCs).
  91. It writes the Mathematica graphics object to a file in a format
  92. accepted by shell scripts which should be run on a machine (Iris or
  93. Next) that has the relevant programs installed. The shell scripts
  94. \"writeoogl\",\"writegv\", and \"writerib\" correspond to WriteOOGL,
  95. Geomview, and WriteRIB respectively: see their usage messages.";
  96.  
  97. DisplayHost::usage= "Option to Geomview[] specifying display hostname
  98. for geomview.  Automatically set to REMOTEHOST or DISPLAY shell
  99. variable if found.  Setting DisplayHost to \"local\" overrides this behavior.\n
  100. To specify a different user name on the remote computer, use\n
  101.     SetOptions[Geomview, DisplayHost -> \"user@host\" ];";
  102.  
  103. GeomviewCommand::usage =
  104. "Option to Geomview[] specifying the command used to start geomview if\n
  105. it is not already running.   May include geomview command-line options.";
  106.  
  107. MachType::usage= "Option to Geomview[] specifying machine type of
  108. DisplayHost.  Should be either \"sgi\" or \"next\".  The default value
  109. is set by Mathematica to be the same as the type of machine on which
  110. Mathematica is running.";
  111.  
  112. (*Geomview /: Options[Geomview] := { DisplayHost -> "" };*)
  113.  
  114. MeshGraphics::usage = "\n
  115. Head for mesh graphics object."
  116.  
  117. Options[Geomview] ^= {DisplayHost->"",
  118.               MachType->If [$System == "NeXT", "next", "sgi"],
  119.               GeomviewCommand -> $GeomviewPath};
  120.  
  121. Begin["`Private`"]
  122.  
  123. (* Use StringJoin not <> for backward compatibility *)
  124.  
  125. WriteChunk[filename_String, gobj_List] := (
  126.   Map[ WriteChunk[filename,#] &, gobj];                    
  127. );
  128.  
  129. WriteChunk[filename_String, gobj_Graphics3D] := (
  130.   Write[filename, CForm[gobj[[0]]]];
  131.   Write[filename, CForm[N[gobj[[1]]]]];
  132. );
  133.  
  134. GoodHeadQ[gobj_] := (SameQ[gobj[[0]],SurfaceGraphics] ||
  135.      SameQ[gobj[[0]],MeshGraphics] || SameQ[gobj[[0]],BezierPlot`BezierPatch])
  136.  
  137. WriteChunk[filename_String, gobj_?GoodHeadQ] := (
  138.   Write[filename, CForm[gobj[[0]]]];
  139.   WriteString[filename, "Dimensions\n"];
  140.   Write[filename, Take[Dimensions[gobj[[1]]],2]];
  141.   If[SameQ[gobj[[0]],SurfaceGraphics],
  142.     WriteString[filename, "MeshRange\n"];
  143.     Write[filename, MeshRange /. gobj[[-1]]]] ;
  144.   (* write out the colors before the points, if they exist *)
  145.   (* Backwards compatibility with v1.2: Dimensions[gobj] returns empty list *)
  146.   If [$VersionNumber >= 2.0,
  147.     If [Dimensions[gobj][[1]] > 2, Write[filename, CForm[gobj[[2]]]]],
  148.     If [Dimensions[Dimensions[gobj[[2]]]][[1]] > 1, 
  149.        Write[filename, CForm[gobj[[2]]]]]];
  150.   Write[filename, CForm[N[gobj[[1]]]]];
  151. );
  152.  
  153. If[$VersionNumber < 2.0, (
  154.     StringTake[str_, take_] := Apply[StringJoin, Take[Characters[str], take]];
  155.     StringPosition[str_, ch_] := Block[{chars = Characters[str]},
  156.     Select[ Table[{i,i}, {i,1,StringLength[str]}], 
  157.         (chars[[ #[[1]] ]] == ch)&] ];
  158.     $Failed = Fail;
  159.   )];
  160.  
  161. BinPrefix[mach_] := StringJoin[ $GeomRoot, "/bin/", mach ];
  162. BinShPrefix[mach_] := StringJoin[ "PATH=$PATH:", BinPrefix[mach], " " ];
  163.  
  164. WriteOut[filename_String, gobj_, postpend_String:"", prepend_String:"",
  165.          options___] :=(
  166.   (* math2oogl expects '{}(), ' to be translated into newlines *)
  167.   tr = "tr -s '{}(), ' '\\12\\12\\12\\12\\12\\12' | ";    
  168.   (* Do the right thing if we've got a command instead of a file name. *)
  169.   tmpfilename =
  170.     If[StringMatchQ[filename, "!*"],
  171.         StringJoin[" | ",StringDrop[filename,1]],
  172.       If[filename != "",
  173.         StringJoin[" > ", filename],
  174.         filename]];
  175.   newfilename =
  176.     StringJoin["!",tr,prepend," ",
  177.            BinShPrefix[ MachType /. {options} /. Options[Geomview]],
  178.            "math2oogl ",postpend, tmpfilename];
  179.   (* We might prepend 'rsh hostname' to the command, which must then be
  180.    * quoted so that what comes after the pipes gets interpreted on
  181.    * the remote not the local machine. The open quote is already in the
  182.    * middle of the prepended string.
  183.    * If we did prepend 'rsh hostname', also add a command to be executed in
  184.    * case the rsh failed, to avoid e.g. mysterious "permission denied" errors.
  185.    * StringMatchQ[prepend,"rsh*"] fails mysteriously in Math 1.2.
  186.    *)
  187.   If[StringLength[prepend]>3 && StringTake[prepend,3] == "rsh",
  188.     newfilename = StringJoin[newfilename,
  189. "'\\' || (echo Geomview graphics: Cannot rsh to machine ", rshhost, " as ",
  190.         If[rshuser != "", StringJoin["user ", rshuser], "yourself"],
  191. ".; echo Check DisplayHost option, or your .rhosts file on ", rshhost, ".)"]];
  192.   OpenWrite[newfilename];    
  193.   WriteChunk[newfilename, gobj];
  194.   Close[newfilename];    
  195.  
  196. );
  197.  
  198. WriteOOGL[filename_String,gobj_] :=(
  199.    WriteOut[filename, gobj];
  200.    gobj
  201. );
  202.  
  203. WriteRIB[filename_String,gobj_, riboptions_String:"", options___] :=(
  204.    WriteOut[filename,gobj, 
  205.       StringJoin[" | oogl2rib -n mma.tiff ",riboptions]];
  206.    gobj
  207. );
  208.  
  209. (*
  210.  * If the given filename is on the $PATH, returns the directory it's in,
  211.  * else returns Null.  Used for load-time warning messages.
  212.  *)
  213. OnPathQ[filename_] :=
  214.     Block[{pdir, i, posen, path = StringJoin[":",Environment["PATH"],":"] },
  215.      (
  216.     posen = StringPosition[path, ":"];
  217.     Do[
  218.       (pdir = StringTake[path, {posen[[i]][[2]]+1, posen[[i+1]][[1]]-1}];
  219.        If[FileType[StringJoin[pdir,"/",filename]] === File,
  220.             Return[pdir]]),
  221.      {i, 1, Length[posen]-1}
  222.     ]
  223.      )];
  224.     
  225.  
  226.  
  227. xhost[display_] := StringTake[display,
  228.    StringPosition[StringJoin[display,":"], ":"] [[1]] [[1]]-1];
  229.  
  230. remotehost[host_String] := (
  231.   newhost="";
  232.   If[host != "",
  233.     newhost = If[host == "local", "", host],
  234.     (
  235.       If[(rshhost = Environment["REMOTEHOST"]) =!= $Failed
  236.      || (rshhost = Environment["DISPLAY"]) =!= $Failed,
  237.       newhost = xhost[rshhost];
  238.       ]
  239.   )];
  240.   If[newhost == $MachineName, newhost = ""];
  241.   If[newhost != "" && Length[StringPosition[newhost, "@"]] == 0 &&
  242.       ((ruser = Environment["REMOTEUSER"]) =!= $Failed
  243.        || (ruser = Environment["USER"]) =!= $Failed
  244.        || (ruser = Environment["LOGNAME"]) =!= $Failed),
  245.     newhost = StringJoin[ruser, "@", newhost]];
  246.   newhost
  247.  );
  248.  
  249. toGV[host_] :=(
  250.   newhost = remotehost[host];
  251.  
  252.   {rshuser, rshhost} =
  253.     If[Length[atposn = StringPosition[newhost, "@"]] > 0,
  254.      {StringTake[newhost, {1,atposn[[1]][[1]]-1}],
  255.     StringTake[newhost, {atposn[[1]][[2]]+1,StringLength[newhost]}]},
  256.      {"", newhost}];
  257.  
  258.   (* Return our rsh command if any, else the empty string. *)
  259.   If[newhost != "",
  260.      StringJoin["rsh ", rshhost, 
  261.         If[StringLength[rshuser] > 0, " -l ", ""], rshuser,
  262.         " /bin/sh -c \\'' DISPLAY=:0 export DISPLAY; "],
  263.      ""
  264.   ]
  265. );
  266.  
  267. Geomview[ name_String, gobj_, options___] := (
  268.    WriteOut["", gobj, StringJoin[" -togeomview ",name," ",
  269.     GeomviewCommand /. {options} /. Options[Geomview]], 
  270.          toGV[DisplayHost /. {options} /. Options[Geomview]],
  271.       options];
  272.    gobj
  273. );
  274.  
  275. Geomview[ gobj_, options___] := (
  276.   Geomview[ "Mathematica", gobj, options ]; gobj
  277. );
  278.  
  279. bpref = BinPrefix[MachType /. Options[Geomview]];
  280.  
  281. If[Not[StringQ[OnPathQ["math2oogl"]]],
  282.   (Print["Warning: the program 'math2oogl' is not on your search path,\n",
  283.     Environment["PATH"], "\n"];
  284.    If[ FileType[StringJoin[bpref, "/math2oogl"]]===File,
  285.       Print["using default path (in OOGL.m) of ", bpref, " instead."],
  286.     Print[
  287. "the Mathematica->geomview graphics package won't be able to work;\n
  288. change the value of $Geomroot in OOGL.m, or ensure that the math2oogl programs\n
  289. are installed somewhere on your search path."]
  290.     ])
  291. ];
  292.  
  293. End[]
  294. EndPackage[]
  295.  
  296. SetOptions[Geomview, DisplayHost -> OOGL`Private`remotehost[""] ];
  297.